package com.snake.springboot.custom.controller.base;

import com.snake.springboot.custom.api.model.base.CustSubAccountModel;
import com.snake.springboot.custom.api.request.base.CustSubAccQuery;
import com.snake.springboot.custom.api.request.base.SubAccPwdReq;
import com.snake.springboot.custom.api.request.page.PageQuery;
import com.snake.springboot.custom.api.service.base.CustSubAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.snake.common.result.ResponseResult;
import org.snake.common.result.ResponseResultFactory;
import org.snake.common.result.ReturnList;
import org.snake.common.utils.IllegalStrUtils;
import org.snake.common.validate.Create;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>商户管理-商户子账号
 * 类{@code CustSubAccount} </br>
 * 创建时间2025/2/8 15:37
 *
 * @author yangxt
 * @see
 */
@Api(tags = "商户管理-商户子账号")
@RestController
@RequestMapping("/cust/sub-acc")
public class CustSubAccountController {

    private CustSubAccountService subAccountService;
    @Autowired
    public CustSubAccountController(CustSubAccountService subAccountService) {
        this.subAccountService = subAccountService;
    }

    @ApiOperation("新增商户子账号")
    @PostMapping("/add")
    public ResponseResult<CustSubAccountModel> addCustSubAccount(
            @ApiParam(name = "接口入参", value = "传入json格式", required = true)
            @RequestBody @Validated({Create.class}) CustSubAccountModel model) {
        IllegalStrUtils.checkIllegalStrNotEmpty(model.getAccount(),"登录名");
        IllegalStrUtils.checkPwd(model.getPassword());
        CustSubAccountModel result = subAccountService.addCustSubAccount(model);
        return ResponseResultFactory.ucc("添加成功！", result);
    }

    @ApiOperation("修改商户子账号")
    @PostMapping("/modify")
    public ResponseResult<CustSubAccountModel> modifyCustSubAccount(
            @ApiParam(name = "接口模型入参", value = "传入json格式", required = true)
            @RequestBody CustSubAccountModel model) {
        subAccountService.modifyCustSubAccount(model);
        return ResponseResultFactory.ucc("修改成功！");
    }

    @ApiOperation("删除商户子账号")
    @GetMapping("/remove/{id}")
    public ResponseResult<Object> remove(
            @ApiParam(name = "记录ID", value = "传入String类型", required = true)
            @PathVariable("id") String id) {
        subAccountService.removeCustSubAccount(id);
        return ResponseResultFactory.ucc("移除成功！");
    }

    @ApiOperation("修改密码商户子账号")
    @PostMapping("/me/update-password")
    public ResponseResult<Object> updatePwdByMe(
            @ApiParam(name = "记录ID", value = "传入String类型", required = true)
            @RequestBody SubAccPwdReq pwdReq) {
        IllegalStrUtils.checkPwd(pwdReq.getPassword());
        subAccountService.updatePwdByMe(pwdReq);
        return ResponseResultFactory.ucc("修改密码成功！");
    }

    @ApiOperation("修改子账号状态")
    @PostMapping("/update-state")
    public ResponseResult<Object> updateState(
            @ApiParam(name = "记录ID", value = "传入String类型", required = true)
            @RequestBody CustSubAccountModel updateState) {
        subAccountService.updateState(updateState);
        return ResponseResultFactory.ucc("修改状态成功！");
    }



    @ApiOperation("管理员修改商户子账号密码")
    @PostMapping("/admin/update-password")
    public ResponseResult<Object> updatePwdByAdmin(
            @ApiParam(name = "记录ID", value = "传入String类型", required = true)
            @RequestBody SubAccPwdReq pwdReq) {
        IllegalStrUtils.checkPwd(pwdReq.getPassword());
        subAccountService.updatePwdByAdmin(pwdReq);
        return ResponseResultFactory.ucc("修改密码成功！");
    }


    @ApiOperation("查询商户子账号列表")
    @PostMapping("/list")
    public ResponseResult<ReturnList<CustSubAccountModel>> queryList(
            @ApiParam(name = "查询条件", value = "传入json类型", required = true)
            @RequestBody PageQuery<CustSubAccQuery> params) {
        if (!StringUtils.isEmpty(params.getCustomId())){
            params.getParams().setCustomId(params.getCustomId());
        }

        ReturnList<CustSubAccountModel> result = subAccountService.queryList(params);
        return ResponseResultFactory.ucc("查询成功！", result);
    }

    @ApiOperation("查询商户子账号列表-不分页")
    @PostMapping("/list-all")
    public ResponseResult<List<CustSubAccountModel>> queryList(
            @ApiParam(name = "查询条件", value = "传入json类型", required = true)
            @RequestBody CustSubAccQuery query) {
        List<CustSubAccountModel> result = subAccountService.queryList(query);
        return ResponseResultFactory.ucc("查询成功！", result);
    }

    @ApiOperation("查询商户子账号详情")
    @GetMapping("/detail/{id}")
    public ResponseResult<CustSubAccountModel> queryCustSubAccountDetail(
            @ApiParam(name = "id", value = "传入String类型", required = true)
            @PathVariable("id") String id) {
        CustSubAccountModel result = subAccountService.queryDetail(id);
        return ResponseResultFactory.ucc("查询成功！", result);
    }

}
